package com.wyy.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wyy.error.BusinessException;
import com.wyy.error.EnumBusinessError;
import com.wyy.pojo.AppreciatesOrder;
import com.wyy.pojo.Users;
import com.wyy.pojo.model.UserModel;
import com.wyy.pojo.model.UserToken;
import com.wyy.response.CommonReturnType;
import com.wyy.service.AlipayService;
import com.wyy.service.OrderService;
import com.wyy.service.UsersService;
import com.wyy.util.FTPUtil;
import com.wyy.util.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-02 11:19
 */
@RestController
public class UserAction {

    @Autowired
    private OrderService orderService;

    @Autowired
    private AlipayService alipayService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private FTPUtil ftpUtil;

    @Autowired
    private TokenUtils tokenUtils;

    private static final String GET_PROVIDER_KEY = "gytcb-user";

    /**
     * 根据用户名称模糊查询用户
     *
     * @param userToken
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod1")
    @RequestMapping(value = "/getUserByNameLike", method = RequestMethod.POST)
    public CommonReturnType getUserByNameLike(@RequestBody UserToken userToken) {
        return CommonReturnType.create(usersService.getUserByNameLike(userToken));
    }

    public CommonReturnType blockHandlerMethod1(UserToken userToken, BlockException e) {
        return CommonReturnType.create("服务限流");
    }


    /**
     * 检查订单支付状态
     *
     * @param appreciatesOrder
     * @return
     */
    @RequestMapping(value = "/checkOrderStatus", method = RequestMethod.POST)
    public CommonReturnType checkOrderStatus(@RequestBody AppreciatesOrder appreciatesOrder) {
        boolean bo = orderService.checkOrderStatus(appreciatesOrder);
        return CommonReturnType.create(bo);
    }


    /**
     * 获取支付二维码
     *
     * @param appreciatesOrder
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getQrCode", method = {RequestMethod.POST})
    public CommonReturnType FacetoFacePay(@RequestBody AppreciatesOrder appreciatesOrder) throws Exception {
        Map<String, Object> map = alipayService.FaceTofACEPay(appreciatesOrder);
        return CommonReturnType.create(map);
    }


    /**
     * 异步回调接口
     */
    @RequestMapping(value = "/notifyUrl", method = RequestMethod.POST)
    public String notifyInterface(@RequestBody Map<String, String> mapParams) throws Exception {
        String map = alipayService.notifyUrl(mapParams);
        return map;
    }

    /**
     * 检查用户是否登录
     *
     * @return
     */
    @RequestMapping(value = "/checkUserIsLogin", method = {RequestMethod.POST})
    public CommonReturnType checkUserIsLogin() {
        String token = tokenUtils.parseTokenToken();
        return CommonReturnType.create(!StringUtils.isEmpty(token));
    }

    /**
     * 获取所有的用户
     *
     * @return
     */
    @RequestMapping(value = "/getAllUser", method = {RequestMethod.POST, RequestMethod.GET})
    public CommonReturnType getAllUser() {
        return CommonReturnType.create(usersService.getAllUsers());
    }

    /**
     * 用户登录接口
     *
     * @param userModel
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod2")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonReturnType login(@RequestBody UserModel userModel) {
        Map<String, Object> map = usersService.login(userModel.getUserNickname(), userModel.getUserPassword());
        return CommonReturnType.create(map);
    }

    public CommonReturnType blockHandlerMethod2(UserModel userModel, BlockException e) {
        return CommonReturnType.create("服务限流");
    }

    /**
     * 退出登录
     *
     * @throws Exception
     */
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public CommonReturnType loginOut() {
        tokenUtils.removeToken();
        return CommonReturnType.create(null);
    }

    /**
     * 验证用户名是否存在
     *
     * @param userNickName
     * @return
     */
    @RequestMapping(value = "/checkUserNickName", method = RequestMethod.POST)
    public CommonReturnType getUserByUserNickname(@RequestBody String userNickName) {
        Map<String, Object> map = usersService.getUserByUserNickname(userNickName);
        return CommonReturnType.create(map);
    }

    /**
     * 验证邮箱是否存在
     *
     * @param userEmail
     * @return
     */
    @RequestMapping(value = "/checkUserEmail", method = RequestMethod.POST)
    public CommonReturnType getUserByUserEmail(@RequestBody String userEmail) {
        Map<String, Object> map = usersService.getUserByUserEmail(userEmail);
        return CommonReturnType.create(map);
    }

    /**
     * 用户注册接口
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    public CommonReturnType registerUser(@RequestBody Users users) {
        Map<String, Object> map = usersService.register(users);
        return CommonReturnType.create(map);
    }


    /**
     * 更新用户头像路径
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/updateUserFilePath", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonReturnType tokenUpdateUserFilePath(MultipartFile file) {
        String filePath = null;
        if (file != null && !file.isEmpty()) {
            UserToken userToken = tokenUtils.parseTokenToUserToken();
            filePath = ftpUtil.uploadFolder(file, userToken.getUserId());
        }
        if (StringUtils.isEmpty(filePath)) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "上传头像失败");
        }
        Users users = new Users();
        UserToken tokenUser = tokenUtils.parseTokenToUserToken();
        BeanUtils.copyProperties(tokenUser, users);
        users.setUserProfilePhoto(filePath);
        usersService.updateUser(users);
        return CommonReturnType.create(null);
    }

    /**
     * 更新用户信息
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public CommonReturnType tokenUpdateUserInfo(@RequestBody Users users) {
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        users.setUserId(userToken.getUserId());
        usersService.updateUser(users);
        return CommonReturnType.create(null);
    }


    /**
     * 验证用户修改密码的时候密码是否正确
     *
     * @param userPassword
     * @return
     */
    @RequestMapping(value = "/checkUserPasswordIsOk", method = RequestMethod.POST)
    public CommonReturnType tokenCheckUserPasswordIsOk(@RequestBody String userPassword) {
        Map<String, Object> map = usersService.userPasswordIsOK(userPassword);
        return CommonReturnType.create(map);
    }

    /**
     * 获取用户的详细信息（不包括密码）
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    public CommonReturnType getUserInfo(@RequestBody String userId) {
        Map<String, Object> map = usersService.getUserById(userId);
        return CommonReturnType.create(map);
    }
}
